<template>
  <el-table :data="tableData" style="width: 100%">
    <el-table-column label="姓名" width="180" prop="username"/>
    <el-table-column label="性别" width="180" prop="sex"/>
    <el-table-column label="年龄" width="180" prop="age"/>  
    
    <el-table-column label="操作">
      <template #default="scope">
        <el-button size="small" 
        @click="handleEdit(scope.row)">
          编 辑
        </el-button>
        <!-- 删除 -->
         <el-popconfirm
            confirm-button-text="确定"
            cancel-button-text="取消"
            :icon="InfoFilled"
            icon-color="#626AEF"
            title="是否删除?"
            @confirm="confirmEvent(scope.row.id)"
            @cancel="cancelEvent"
          >
            <template #reference>
              <el-button type="danger">删 除</el-button>
            </template>
          </el-popconfirm>
      </template>
    </el-table-column>
  </el-table>
  <!-- 分页组件 -->
  <el-pagination
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :page-sizes="[5,10,15]"
      :size="size"
      :disabled="disabled"
      :background="background"
      layout="total, sizes, prev, pager, next, jumper"
      :total="title"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
</template>

<script  setup>
import { onMounted, ref } from 'vue'
import axios from 'axios'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'

//数据集
const tableData = ref([])
const currentPage = ref(1)
const pageSize = ref(5)
const size = ref('mini')
const title = ref(0)

const handleSizeChange = (val) => {
  showData(currentPage.value,val)
}
const handleCurrentChange = (val) => {
  showData(val,pageSize.value)
}

//显示数据
const showData = (currentPage,pageSize) => {
  axios.post('/api/show.do',{page:currentPage,size:pageSize})
  .then(res => {
    console.log('------->>',res);
    tableData.value = res.data.list
    title.value = res.data.rows
  })
}

onMounted(() => {
  showData(currentPage.value,pageSize.value)
})

const router = useRouter()

const handleEdit = (row) => {
  router.push({
    path: '/saveUser',
    state: {
      person:JSON.stringify(row)
    }
  })
}

//------------删除-----------
const confirmEvent = (id) => {
  axios.post('/api/delete.do',{id:id,flag:"del"})
  .then(() => {
    ElMessage({
      message: '删除成功.',
      type: 'success',
    })
    //重新加载数据
    showData(currentPage.value,pageSize.value)
  })

  
}
const cancelEvent = () => {
  ElMessage({
    message: '取消删除.',
    type: 'info',
  })
}


</script>
